package ThreadPool.lagou.concurrent.cyclicBarrier.demo01;

import java.util.Random;
import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;

/**
 * @author oyz
 * @version 1.0.4
 * @description: 用CyclicBarrier实现线程之间的同步等待
 * @date 2022/10/5 16:34
 */
public class MyThread extends Thread{

    private final CyclicBarrier barrier;

    private final Random random = new Random();


    public MyThread(String name,CyclicBarrier barrier) {
        super(name);
        this.barrier = barrier;
    }

    @Override
    public void run() {
        try {
            System.out.println(Thread.currentThread().getName() + "向公司出发");
            sleep(random.nextInt(2000));
            System.out.println(Thread.currentThread().getName() + "到达公司");
            // 等待其他线程结束
            barrier.await();
            System.out.println(Thread.currentThread().getName() + "开始笔试");
            sleep(random.nextInt(2000));
            System.out.println(Thread.currentThread().getName() + "笔试结束");
            barrier.await();
            System.out.println(Thread.currentThread().getName() + "开始笔试");
            sleep(random.nextInt(2000));
            System.out.println(Thread.currentThread().getName() + "面试结束");
        } catch (Exception e ) {
            throw new RuntimeException(e);
        }
    }
}
